﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using PhotoHsn.WebApp.Core;
using PhotoHsn.BLL.Adapters;
using PhotoHsn.BLL.Authantication;
using PhotoHsn.BLL.Core;
using Bxas.Utility.Web.Keepers;
using System.Diagnostics;
using Bxas.Core.Exceptions;

namespace PhotoHsn.Web
{
    public partial class ForgotPassword : BasePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                load_ddlSecQues();
            }
        }

        private void load_ddlSecQues()
        {
            SecurityQuestionsBs scbs = new SecurityQuestionsBs();
            try
            {
                List<SecurityQuestions> list = scbs.get_all_actives();
                ddlSecQues.DataSource = list;
                ddlSecQues.DataTextField = "Text";
                ddlSecQues.DataValueField = "Id";
                ddlSecQues.DataBind();
                ddlSecQues.Items.Insert(0, new ListItem("Seçiniz", "0"));
            }
            catch (Exception ex)
            {
                MessageShow(ex.Message, EnumMessageTypes.Hata);
            }

        }
        protected void lnkBtnGetSecQues_Click(object sender, EventArgs e)
        {
            MembersBs membersBs = new MembersBs();
            Members member;
            try
            {
                member = membersBs.get_for_ForgotPassword(txtEmail.Text);
                SessionKeeper.add(SessionNames.ForgotMember, member);

                if (member == null)
                {
                    MessageShow("Bu Email adresi ile sistemde kullanıcı bulunmamaktadır.", EnumMessageTypes.Uyari);
                }
                else
                {
                    tblGetSecQues.Visible = false;
                    tblSecQues.Visible = true;
                    txtEmail.Enabled = false;
                    ddlSecQues.SelectedValue = member.SecurityQuestionId.ToString();
                    ddlSecQues.Enabled = false;
                }
            }
            catch (Exception ex)
            {
                MessageShow(ErrorLogInsert(ex, this, new StackFrame()), EnumMessageTypes.Hata);
            }
        }

        protected void lnkBtnSubmit_Click(object sender, EventArgs e)
        {
            Members member = (Members)SessionKeeper.get_value(SessionNames.ForgotMember);
            if (member != null)
            {
                try
                {
                    if (member.SecurityResponse == txtSecAnswer.Text)
                    {
                        MembersBs memberBs = new MembersBs();
                        member.Password = Bxas.Utility.Security.MD5Operations.convert_string_to_MD5(txtNewPassword.Text);
                        memberBs.update(member);
                        SessionKeeper.add(SessionNames.Message, "Şifreniz değişmiştir. Yeni şifreniz ile sisteme giriş yapabilirsiniz.");
                        SessionKeeper.add(SessionNames.EnumMessageTypes, EnumMessageTypes.Bilgi);
                        Response.Redirect("~/Default.aspx");
                    }
                    else
                    {
                        MessageShow("Güvenlik sorusuna verdiğiniz cevap yanlış. Lütfen Tekrar Deneyiniz.",EnumMessageTypes.Uyari);
                    }

                }
                catch (Exception ex)
                {
                    MessageShow(ErrorLogInsert(ex, this, new StackFrame()), EnumMessageTypes.Hata);
                }
            }
        }
    }
}